package com.karry.yl.web.system;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.karry.commons.base.SuperController;
import com.karry.commons.vo.JsonResponse;
import com.karry.commons.vo.LayTableResponse;
import com.karry.yl.domain.system.Role;
import com.karry.yl.domain.system.User;
import com.karry.yl.service.system.LoginService;
import com.karry.yl.service.system.RoleService;
import com.karry.yl.service.system.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController extends SuperController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private LoginService loginService;


    /**
     * 用户管理添加页
     * @return
     */
    @RequestMapping("/ui/add")
    public String addUI(ModelMap modelMap){
        List<Role> roles = roleService.selectList(new EntityWrapper<Role>().eq("orgId",currentUser.getOrgId()).eq("deleted",false));
        modelMap.put("roles",roles);
        return "system/user/add";
    }

    /**
     * 用户管理添加页
     * @return
     */
    @RequestMapping("/ui/edit")
    public String editUI(Long id, ModelMap modelMap){
        User user = userService.selectById(id);
        List<Role> roles = roleService.selectList(new EntityWrapper<Role>().eq("orgId",currentUser.getOrgId()).eq("deleted",false));
        modelMap.put("roles",roles);
        modelMap.put("user",user);
        return "system/user/edit";
    }

    /**
     * 用户管理列表页
     * @return
     */
    @RequestMapping("/ui/list")
    public String listUI(){
        return "system/user/list";
    }



    @RequestMapping("/list")
    @ResponseBody
    public LayTableResponse list(int page, int limit){
        Page<User> resourcesPage = userService.selectUserPage(new Page<User>(page, limit),currentUser.getOrgId(),null);
        return new LayTableResponse(resourcesPage.getRecords(),resourcesPage.getTotal());
    }

    /**
     *
     * @param user
     * @return
     */
    @RequestMapping("/save")
    public String save(User user){
        user.setOrgId(currentUser.getOrgId());
        user.setPassword(DigestUtils.md5Hex(user.getPassword()));
        user.setPrimary(false);
        userService.insert(user);
        return "redirect:/user/ui/list";
    }

    /**
     *
     * @param user
     * @return
     */
    @RequestMapping("/update")
    public String update(User user){
        userService.update(user);
        return "redirect:/user/ui/list";
    }


    /**
     * 删除
     * @param
     * @return
     */
    @RequestMapping(path = "/delete")
    public String delete(@RequestParam("id") List<Long> ids){
        userService.deleteBatchIds(ids);
        return "redirect:/user/ui/list";
    }


    /**
     * 启用/禁用
     * @status  1:启用  0 ：禁用
     * @return
     */
    @RequestMapping(path = "/status")
    public String delete(Long id){
        userService.modifyStatus(id);
        return "redirect:/user/ui/list";
    }


    /**
     * 变更角色界面
     * @id
     * @return
     */
    @RequestMapping(path = "/ui/changerole")
    public String changeRoleUI(Long id,ModelMap modelMap){
        User user = userService.selectById(id);
        List<Role> roles = roleService.selectList(new EntityWrapper<Role>().eq("orgId",currentUser.getOrgId()).eq("deleted",false));
        modelMap.put("roles",roles);
        modelMap.put("user",user);
        return "system/user/changerole";
    }

    /**
     * 修改角色
     * @param id
     * @param roleId
     * @return
     */
    @RequestMapping(path = "/changerole")
    @ResponseBody
    public JsonResponse changeRole(Long id, Long roleId){
        userService.modifyRole(id,roleId);
        return JsonResponse.success("OK");
    }


    @RequestMapping(path = "/exists")
    @ResponseBody
    public JsonResponse exists(String userCode,String phone,Long id){
        JsonResponse rs;
        Wrapper<User> wrapper = new EntityWrapper<User>().eq("deleted",false);
        if(null != id){
            wrapper.ne("id",id);
        }
        if(null != phone){
            wrapper.eq("phone", phone);
        }
        if(null != userCode){
            wrapper.eq("userCode", userCode);
        }
        User u = userService.selectOne(wrapper);
        if(null == u){
            rs = JsonResponse.success("OK");
        }else {
            rs = JsonResponse.fail("手机号已存在");
        }
        return rs;
    }

    @RequestMapping("/changepwd")
    public String changepwd(){
        return "changepwd";
    }

    @RequestMapping("/change")
    @ResponseBody
    public JsonResponse changepwd(String oldpwd,String newpwd){
        User u = loginService.login(currentUser.getPhone(), oldpwd);
        if(null == u){
            return JsonResponse.fail("原始密码错误");
        }
        //修改密码
        u.setPassword(DigestUtils.md5Hex(newpwd));
        userService.updateById(u);
        return JsonResponse.success("修改成功");
    }
    /**
     * 查询用户状态
     * @param id
     * @return
     */
    @RequestMapping(path = "/selectUser")
    @ResponseBody
    public JsonResponse selectUser(Long id){
        User user = userService.selectById(id);
        return JsonResponse.success(user);
    }

}
